package cn.bluedot.framework.web;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.bluedot.framework.annotations.Dispatcher;
import cn.bluedot.framework.annotations.Redirect;
import cn.bluedot.framework.annotations.RequestView;
import cn.bluedot.framework.annotations.Service;
import cn.bluedot.framework.exception.ServiceException;
import cn.bluedot.framework.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HandlerManager {

    public static Map<String,RequestHandler> handlerMap = new HashMap<>();

    private static Logger logger = LoggerFactory.getLogger(HandlerManager.class);

    /**
     * 	初始化MappingHandler
     * @param classList
     */
    public static void resolveMappingHandler(List<Class<?>> classList){
        for(Class<?> cls : classList){
            if(cls.isAnnotationPresent(Service.class)){  //如果含有Service注解
                parseHandlerFromController(cls); //解析Service类
            }
        }
    }

    /**
     * 	将Service中含有RequestView注释,@Redirect,@Dispatcher的方法进行封装
     * @param cls  当前Class
     */
    private static void parseHandlerFromController(Class<?> cls){
        Method[] methods = cls.getDeclaredMethods(); //获取所有方法
        
        String uri_cls = null; 
        
        if(cls.isAnnotationPresent(RequestView.class)) { //获取类上的RequestView的值
        	uri_cls = cls.getDeclaredAnnotation(RequestView.class).uri();
        }
        
        for(Method method : methods){
            
        	if(!method.isAnnotationPresent(RequestView.class)){ //如果没有RequestMapping注解
                continue;
            }
            
            String value = method.getDeclaredAnnotation(RequestView.class).uri();//方法上的RequestMapping值
            
            String uri = uri_cls == null ? value : (uri_cls + value);

            logger.debug(uri);

            String success = null;
            String failed = null;
            
            if(method.isAnnotationPresent(Redirect.class)) {
            	Redirect redirect = method.getDeclaredAnnotation(Redirect.class);
            	success = redirect.success();
            	failed = redirect.failed();
            }
            
            if(method.isAnnotationPresent(Dispatcher.class)) {
            	Dispatcher dispatcher = method.getDeclaredAnnotation(Dispatcher.class);
            	success = dispatcher.success();
            	failed = dispatcher.failed();
            }
            
            RequestHandler mappingHandler = new RequestHandler(uri, method, cls, success, failed);
            
            HandlerManager.handlerMap.put(uri, mappingHandler);
        }
    }
    
  
    /**
     * 根据ServletRequest判断URI是否存在
     * @param req
     * @return
     * @throws ServiceException
     */
    public static RequestHandler getHandler(String uri) {
    	
		return handlerMap.get(uri);
    }
    
    /**
     * 通过URI获取对应的方法
     * @param uri
     * @return
     */
    public static Method getMethod(String uri) {
    	return  handlerMap.get(uri).getMethod();
    }
       
}
